//无头双向链表实现
class MyLinkedLists {
    //定义类部类
    static class ListNode{
       public int val;
       public ListNode prev;
       public ListNode next;

        public ListNode(int val) {
            this.val = val;
        }
    }
    public ListNode head;
    public ListNode last;

    //头插法
    public void addFirst(int data){
     ListNode cur=new ListNode(data);
        if(head==null){
            head=last=cur;
        }else {
            cur.next = head;
            head.prev = cur;
            head = cur;
        }
    }
    //尾插法
    public void addLast(int data){
        ListNode cur=new ListNode(data);
        if(head==null){
            head=last=cur;
        }else{
            last.next=cur;
            cur.prev=last;
            last=cur;
        }
    }
    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index,int data){
        int len=size();
        if(index>len||index<0){
            return;
        }if(index==0){
            addFirst(data);
            return;
        }if(index==len){
            addLast(data);
            return;
        }else {
            ListNode code = new ListNode(data);
            ListNode cur = isfind(index);//插入结点位置
            code.next = cur;
            cur.prev.next = code;
            cur = code;
            code = cur.prev;
        }
    }
    private ListNode isfind(int index){
        ListNode cur=head;
        while(index!=0){
            cur=cur.next;
            index--;
        }
        return cur;
    }
    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key){
        if(head==null);
        while(head!=null){
            head=head.next;
            if(head.val==key){
                return true;
            }
        }
        return false;
    }

    //删除第一次出现关键字为key的节点
    public void remove(int key){
     ListNode h=head;
     if(h==null)return;
     while(h!=null){
         if(h.val==key){
          //开始删除
             if(h==head){
                h=h.next;
                if(head!=null){
                h.prev=null;
                }
             }else {
                 h.prev.next=h.next;
                 if (h.next==null) {
                    //尾结点
                     h=h.prev;
                 }else{
                    h.next.prev=h.prev;
                 }
             }
             return;
         }else {
             h = h.next;
         }
     }
    }




    //删除所有值为key的节点
    public void removeAllKey(int key){
        ListNode h=head;
        if(h==null)return;
        while(h!=null){
            if(h.val==key){
                //开始删除
                if(h==head){
                    head=head.next;//真正要变在head，所以这使用head而不是h
                    if(head!=null){
                        head.prev=null;
                    }
                }else {
                    h.prev.next=h.next;
                    if (h.next==null) {
                        //尾结点
                        h=h.prev;
                    }else{
                        h.next.prev=h.prev;
                    }
                }
            }
            h = h.next;
        }
    }

    //得到单链表的长度
    int len=0;
    public int size(){
        ListNode a=head;
        if(a==null)return 0;
        while(a!=null){
            len++;
            a=a.next;
        }
        return len;
    }

    public void display(){

        while(head!=null){
            System.out.print(head.val+" ");
            head=head.next;
        }
    }

    public void clear(){
        if(head==null)return;
        while(head!=null){
            head=head.next;
            head=null;
        }
        head=null;
    }
}
public class lianbiaoss {
    public static void main(String[] args) {
        MyLinkedLists mylist=new MyLinkedLists();
        mylist.addLast(1);
        mylist.addLast(1);
        mylist.addLast(1);
        mylist.addLast(4);
        mylist.addLast(5);
        //mylist.addIndex(1,0);
        //mylist.remove(5);
        mylist.removeAllKey(1);
        mylist.display();
        //System.out.println(mylist.contains(2));
        //System.out.println(mylist.size());
        //mylist.clear();
    }
}
